package com.qf.io.serverSocket;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Description:
 * @Author: xbb
 * @Date:2020/4/16
 */
public class SocketDemo {
    public static void main(String[] args) {
        Socket socket = null;
        try {
            //创建一个socket对象，连接服务器
            socket = new Socket("localhost",8080);

            //发送消息给服务端
            socket.getOutputStream().write("hello".getBytes("utf-8"));

            //接收服务端发送过来的消息
            InputStream inputStream = socket.getInputStream();
            Scanner scanner = new Scanner(inputStream);
            while (scanner.hasNextLine()){
                System.out.println(scanner.nextLine());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
